/* Doesnt' work. Why? */
//AjaxLab.Engine.Plugins.BaseCommands.callBaseMethod(this, 'registerCommands',[customCommands]);

/*
		Will it be better to use delegate and have users define their own method instead? lik
		this.registerCommandProcess(custoCommand, commandCallBack, commandCallBackContext);
		
		CMDUtil.js is avaiable for plug in classes to use. - this.Console
*/
	
Type.registerNamespace("AjaxLab.Engine.Plugins");
AjaxLab.Engine.Plugins.MSIISLogParser= function()
{
	var customCommands = "LogParser|WebStats";
	var instance = this;
	AjaxLab.Engine.Plugins.BaseCommands.initializeBase(this);
	this.registerCommands(customCommands);
	
	/* Implement the Plug In here */
	
	this.processCommands = function(inputtext) {
		instance.Log4J.setRootLevel(instance.Log4J.LevelTypes.Debug);
		instance.Log4J.Debug("LogParser command detected and input string is :"+inputtext);
		var command     = inputtext.split(" ");
		if (command == null)
		{
			instance.Log4J.Debug("LogParser Command null");
		}
		instance.Log4J.Debug("Logparser input string command[0]:"+command[0]);
		if(command[0].toUpperCase() == "LOGPARSER" || "WEBSTATS")
		{
			if(command[1].toUpperCase() == "TOP")
			{
				if(command[2].toUpperCase() =="VISITORS")
				{
					instance.Log4J.Debug("Start retrieving Top IPCount");
					AjaxLabCmd.CMDLogParserDemo.SelectTopVisits(command[3],this.IPCountCallBack);
				}
				else if(command[2].toUpperCase() =="HITS")
				{
					AjaxLabCmd.CMDLogParserDemo.TopHits(command[3],this.TopHitsCallBack);
				}
				else if(command[2].toUpperCase() =="PAGEERRORS")
				{
					AjaxLabCmd.CMDLogParserDemo.TopPageErrors(command[3],this.TopPageErrorsCallBack);
				}
				else if(command[2].toUpperCase() =="POSSIBLEATTACKS")
				{
					AjaxLabCmd.CMDLogParserDemo.TopPossibleAttacks(command[3],this.TopPossibleAttacksCallBack);
				}
				else if(command[2].toUpperCase() =="HITSPERPAGE")
				{
					AjaxLabCmd.CMDLogParserDemo.TopIPHitsPerPage(command[3],this.TopIPHitsPerPageCallBack);
				}
				else if(command[2].toUpperCase() =="RECORDEDERRORS")
				{
					AjaxLabCmd.CMDLogParserDemo.TopRecordedErrors(command[3],this.TopRecordedErrorsCallBack);
				}
				else if(command[2].toUpperCase() =="BROKENLINKS")
				{
					AjaxLabCmd.CMDLogParserDemo.TopBrokenLinks(command[3],this.TopBrokenLinksCallBack);
				}
				else if(command[2].toUpperCase() =="WIN32ERRORS")
				{
					AjaxLabCmd.CMDLogParserDemo.TopWin32Errors(command[3],this.TopWin32ErrorsCallBack);
				}
				else if(command[2].toUpperCase() =="HTTPMETHODS")
				{
					AjaxLabCmd.CMDLogParserDemo.TopPageHttpMethods(command[3],this.TopPageHttpMethods);
				}
				else if(command[2].toUpperCase() =="BYTESSENDTOCLIENT")
				{
					AjaxLabCmd.CMDLogParserDemo.TopBytesSendToClient(command[3],this.TopBytesSendToClientCallBack);
				}
				else if(command[2].toUpperCase() =="BYTESSENDFROMCLIENT")
				{
					AjaxLabCmd.CMDLogParserDemo.TopBytesSendFromClient(command[3],this.TopBytesSendFromClientCallBack);
				}
				else if(command[2].toUpperCase() =="TIMESPENDPROCESSING")
				{
					AjaxLabCmd.CMDLogParserDemo.TopTimeSpentProcessing(command[3],this.TopTimeSpentProcessingCallBack);
				}
				else if(command[2].toUpperCase() =="NONSTANDARDUSERAGENT")
				{
					AjaxLabCmd.CMDLogParserDemo.TopNonStandardUserAgent(command[3],this.TopNonStandardUserAgentCallBack);
				}
				else{
					instance.Console.printToConsole(command[2]+" is not recognized as an internal or external command");
				}
			}
			else if(command[1].toUpperCase() == "SUCCESSFUL")
			{
				var failed = false;
				if(command[2].toUpperCase() =="ATTEMPTS")
				{
					AjaxLabCmd.CMDLogParserDemo.SelectSuccessfulAttemps(command[3],this.SuccessfulAttemptCallBack);
				}
				else{
					failed = true;
				}
				if(failed==true)
				{
					instance.Console.printToConsole(inputtext+" is not recognized as an internal or external command");
				}
			}
			else if(command[1].toUpperCase() == "COMPARE")
			{
				if(command[2].toUpperCase() =="IPSUSERAGENT")
				{
					if(command[3]==null || command[4]==null)
					{
						instance.Console.printToConsole("You need to IP address to compare sepeparted by space");
					}
					else{
						AjaxLabCmd.CMDLogParserDemo.CompareIPsUserAgent(command[3], command[4], this.ComparesIpsUserAgentCallBack);
					}
				}
			}
			else{
				instance.Log4J.Debug("LogParser command :"+command[1]+" not recognized");
				instance.Console.printToConsole(inputtext+" is not recognized as an internal or external command");
			}
		}
		else{
				instance.Log4J.Debug("LogParser base command not found");
				instance.Console.printToConsole(inputtext+" is not recognized as an internal or external command");
		}
    }
    
    this.ComparesIpsUserAgentCallBack	=	function(res)
    {
		instance.Log4J.Debug("Start Top Viists Print");
		instance.Console.println("");
		instance.Console.println("IP			Agent");
		instance.Console.println("----------		----------");
		
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].IP + "             " + res.value.Tables[0].Rows[i].Agent);
			
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("Endclear Top Visits Print");
    }
    
    this.IPCountCallBack = function(res)
    {	
		instance.Log4J.Debug("Start Top Viists Print");
		instance.Console.println("");
		instance.Console.println("IP			Count");
		instance.Console.println("----------		----------");
		
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].IP + "              : " + res.value.Tables[0].Rows[i].Total);
			
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("Endclear Top Visits Print");
		
    }
   
	this.SuccessfulAttemptCallBack = function(res)
	{
		instance.Log4J.Debug("Start Successful Attempts Print");
		instance.Console.println("");
		instance.Console.println("URL							Hits");
		instance.Console.println("----------						----------");
		
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].URL + "				        : " + res.value.Tables[0].Rows[i].Hits);
			
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End Successful Attempts Print");
	}
	
	this.TopHitsCallBack			=	function(res)
	{
		instance.Log4J.Debug("Start Successful Attempts Print");
		instance.Console.println("");
		instance.Console.println("Day			PageName				Hits");
		instance.Console.println("----------	------------------				----------");
		
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].Day + "	" + res.value.Tables[0].Rows[i].PageName+ "		"+ res.value.Tables[0].Rows[i].Total);
			
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End Successful Attempts Print");
	}
	
	this.TopPageErrorsCallBack		=	function(res)
	{
		instance.Log4J.Debug("Start Page Errors Print");
		instance.Console.println("");
		instance.Console.println("Date		Hours		Status		Errors");
		instance.Console.println("----------	----------    ----------	----------");
		
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].Date + "	" + res.value.Tables[0].Rows[i].hour+ "		"+ res.value.Tables[0].Rows[i].status+ "		"+ res.value.Tables[0].Rows[i].errors);
			}
			if(res.value.Tables[0].Rows.length==0)
			{
				instance.Console.println("Congruatulation! You don't have any error pages");
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End Page Errors Print");
	}
	
	this.TopPossibleAttacksCallBack		=	function(res)
	{
		instance.Log4J.Debug("Start Possible Attacks Print");
		instance.Console.println("");
		instance.Console.println("Day			PageName				Hits");
		instance.Console.println("----------	------------------				----------");
	
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].IP + "	" + res.value.Tables[0].Rows[i].PageName+ "		"+ res.value.Tables[0].Rows[i].Hits);
			}
			if(res.value.Tables[0].Rows.length==0)
			{
				instance.Console.println("Congruatulation! You don't have any attacks");
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End Possible Attacks Print");
	}
	
	this.TopIPHitsPerPageCallBack	=	function(res)
	{
		instance.Log4J.Debug("Start IP Hits Per Page Print");
		instance.Console.println("");
		instance.Console.println("Date		PageName		IP		Hits");
		instance.Console.println("----------	----------    ----------	----------");
		
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].date + "	" + res.value.Tables[0].Rows[i].PageName+ "		"+ res.value.Tables[0].Rows[i].IP+ "		"+ res.value.Tables[0].Rows[i].Hits);
			}
			if(res.value.Tables[0].Rows.length==0)
			{
				instance.Console.println("No Page has hits over 50! ");
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End IP Hits Per Page Print");
	}
	
	this.TopRecordedErrorsCallBack	=	function(res)
	{
		instance.Log4J.Debug("Start Recorded Errors Print");
		instance.Console.println("");
			instance.Console.println("URL							Total");
		instance.Console.println("----------						----------");
		
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].URIQuery + "							 " + res.value.Tables[0].Rows[i].Total);
			
			}
			if(res.value.Tables[0].Rows.length==0)
			{
				instance.Console.println("Congruatulation! You don't have any recorded errors!");
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End Recorded Errors Print");
	}
	this.TopBrokenLinksCallBack		=	function(res)
	{
		instance.Log4J.Debug("Start Broken Links Print");
		instance.Console.println("");
		instance.Console.println("Date		Hour		Status		Errors");
		instance.Console.println("----------	----------    ----------	----------");
		
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].date + "	" + res.value.Tables[0].Rows[i].hour+ "		"+ res.value.Tables[0].Rows[i].Status+ "		"+ res.value.Tables[0].Rows[i].Errors);
						}
			if(res.value.Tables[0].Rows.length==0)
			{
				instance.Console.println("Congruatulation! You don't have any broken links with http status 400");
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End  Broken Links Print");
	}
	this.TopWin32ErrorsCallBack		=	function(res)
	{
		instance.Log4J.Debug("Start Win32 Errors Print");
		instance.Console.println("");
		instance.Console.println("URI			Error				Total");
		instance.Console.println("----------	------------------			----------");
	
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].URI + "	" + res.value.Tables[0].Rows[i].Error+ "		"+ res.value.Tables[0].Rows[i].Total);
			}
			if(res.value.Tables[0].Rows.length==0)
			{
				instance.Console.println("Congruatulation! You don't have any window 32 errors");
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End Win32 Errors Print");
	}
	this.TopPageHttpMethods			=	function(res)
	{
		instance.Log4J.Debug("Start HttpMethod Print");
		instance.Console.println("");
		instance.Console.println("URI					Method				Total");
		instance.Console.println("----					------				------");
	
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].URI + "	" + res.value.Tables[0].Rows[i].Method+ "				"+ res.value.Tables[0].Rows[i].Total);
			}
			if(res.value.Tables[0].Rows.length==0)
			{
				instance.Console.println("Weird.  You don't have any page in yoru log?");
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End Http Method Print");
	}
	this.TopBytesSendToClientCallBack	=	function(res)
	{
		
		instance.Log4J.Debug("Start Bytes Send to Client Print");
		instance.Console.println("");
		instance.Console.println("URI	Hits	Avg		Max		Min		Total");
		instance.Console.println("---	---		---		---		---		-----");
	
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].URI + "	" + res.value.Tables[0].Rows[i].Hits+ "	"+ res.value.Tables[0].Rows[i].Avg+ "	"+ res.value.Tables[0].Rows[i].Max+ "	"+ res.value.Tables[0].Rows[i].Min+ "	"+ res.value.Tables[0].Rows[i].Total);
			}
			if(res.value.Tables[0].Rows.length==0)
			{
				instance.Console.println("Weird.  You don't have any page in yoru log?");
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End Bytes Send to Client Print");
	
	}
	this.TopBytesSendFromClientCallBack	=	function(res)
	{
		instance.Log4J.Debug("Start Bytes Send From Client Print");
		instance.Console.println("");
		instance.Console.println("URI	Hits	Avg		Max		Min		Total");
		instance.Console.println("---	---		---		---		---		-----");
	
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].URI + "	" + res.value.Tables[0].Rows[i].Hits+ "	"+ res.value.Tables[0].Rows[i].Avg+ "	"+ res.value.Tables[0].Rows[i].Max+ "	"+ res.value.Tables[0].Rows[i].Min+ "	"+ res.value.Tables[0].Rows[i].Total);
			}
			if(res.value.Tables[0].Rows.length==0)
			{
				instance.Console.println("Weird.  You don't have any page in yoru log?");
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End Bytes Send From Client Print");
	}
	this.TopTimeSpentProcessingCallBack	=	function(res)
	{
		instance.Log4J.Debug("Start Time Spend on Processing Print");
		instance.Console.println("");
		instance.Console.println("URI	Hits	Avg		Max		Min		Total");
		instance.Console.println("---	---		---		---		---		-----");
	
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].URI + "	" + res.value.Tables[0].Rows[i].Hits+ "	"+ res.value.Tables[0].Rows[i].Avg+ "	"+ res.value.Tables[0].Rows[i].Max+ "	"+ res.value.Tables[0].Rows[i].Min+ "	"+ res.value.Tables[0].Rows[i].Total);
			}
			if(res.value.Tables[0].Rows.length==0)
			{
				instance.Console.println("Weird.  You don't have any page in yoru log?");
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End Time Spend On Processing  Print");
	}
	
	this.TopNonStandardUserAgentCallBack	=	function(res)
	{
		instance.Log4J.Debug("Start Non Standard User Agent Print");
		instance.Console.println("");
		instance.Console.println("Agent");
		instance.Console.println("-----");
		
		if(res.value.Tables[0]!=null)
		{		
			for(var i=0; i<res.value.Tables[0].Rows.length; i++)
			{
				instance.Console.println(res.value.Tables[0].Rows[i].Agent);
			
			}
		}
		else{
			instance.Console.println("No values");
		}
		instance.Console.newPrompt();
		instance.Log4J.Debug("End Non Standard User Agent Print");
	}
}
AjaxLab.Engine.Plugins.MSIISLogParser.registerClass('AjaxLab.Engine.Plugins.MSIISLogParser', AjaxLab.Engine.Abstract.Commands);




